package cn.com.shadowless.xinning.view.activity;

import android.content.Intent;
import android.os.Build;
import android.util.Log;

import java.util.concurrent.TimeUnit;

import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseActivity;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * The type Splash activity.
 *
 * @author sHadowLess
 */
public class SplashActivity extends BaseActivity {

    private final String TAG = SplashActivity.class.getSimpleName();

    @Override
    protected int setLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void setData() {
        Observable.timer(3, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    Disposable disposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        String userName = PreferencesUtils.getString(SplashActivity.this, "userName", "-1");
                        if (!"-1".equals(userName)) {
                            startActivity(new Intent(SplashActivity.this, OrderActivity.class));
                        } else {
                            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        }
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }
}